package com.example.game_dafangdaxie.View;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.game_dafangdaxie.R;
import com.example.game_dafangdaxie.StringConstant.Point;
import com.example.game_dafangdaxie.StringConstant.TouchList;
import com.example.game_dafangdaxie.StringConstant.TouchXY;
import com.example.game_dafangdaxie.StringConstant.WINSTATUS;
import com.example.game_dafangdaxie.algorithm.ConditionalJudgment;
import com.example.game_dafangdaxie.algorithm.JudgmentWin;
import com.example.game_dafangdaxie.algorithm.changePiece;
import com.example.game_dafangdaxie.resUtils.mSoundPool;

import java.util.ArrayList;

/***
 * 游戏页面
 * create by LiuTong
 * 2021.9.26
 */

@SuppressLint("ViewConstructor")
public class ChessView extends SurfaceView implements SurfaceHolder.Callback, Runnable, View.OnTouchListener {
    //是否亮屏
    private boolean isDraw;
    //行
    private final static int ROW=6;
    private final static int COL=6;
    //屏幕宽
    private  static float WIDTH;
    private  static float HEIGHT;
    private Drawable background;
    private SurfaceHolder surfaceHolder;
    //棋盘长度
    private static float length;
    //棋盘最上面离顶部距离
    private static float DistanceTop;
    //棋盘里左侧距离
    private static final float DistanceLeft=50f;
    //棋盘间隔
    private static float Spacing;
    private final Context context;
    //总棋盘数组
    private  ArrayList<TouchXY> touchXYList;
    //红方玩家
    private static int play=0;
    //步数
    private static int steps=1;
    private static Point startPoint;
    //奖励步数
    private static int RewardSteps=0;

    private mSoundPool mSoundPool;

    private Drawable btn_blue_fail,btn_red_fail;
    private Drawable blue_1,blue_2,blue_3,add_blue_1,add_blue_2,add_blue_3;

    private float red_fail_minX,red_fail_maxX,red_fail_minY,red_fail_maxY;
    private float blue_fail_minX,blue_fail_maxX,blue_fail_minY,blue_fail_maxY;
    private static int xSteps = 0,ySteps = 0;

    /***
     * ValidClickAreaLength 有效点击长度
     */
    private static float minX,minY,maxX,maxY,ValidClickAreaLength;
    private static ArrayList<Float> XList=new ArrayList<>();
    private static ArrayList<Float> YList=new ArrayList<>();



    public ChessView(Context context,Boolean isDraw) {
        super(context);
        this.isDraw=isDraw;
        this.setFocusable(true);
        this.context=context;
        this.setFocusableInTouchMode(true);
        initGame();
    }

    private void initGame() {
        touchXYList=new ArrayList<>();
        surfaceHolder=getHolder();
        surfaceHolder.addCallback(this);
        background = getResources().getDrawable(R.drawable.startbg, null);
        btn_blue_fail=getResources().getDrawable(R.drawable.blue_fail,null);
        btn_red_fail=getResources().getDrawable(R.drawable.red_fail,null);
        blue_1=getResources().getDrawable(R.drawable.blue_1_steps,null);
        blue_2=getResources().getDrawable(R.drawable.blue_2_steps,null);
        blue_3=getResources().getDrawable(R.drawable.blue_3_steps,null);
        add_blue_1=getResources().getDrawable(R.drawable.add_blue_1,null);
        add_blue_2=getResources().getDrawable(R.drawable.add_blue_2,null);
        add_blue_3=getResources().getDrawable(R.drawable.add_3_blue,null);
        mSoundPool=new mSoundPool(context);
        setOnTouchListener(this);
        setFocusable(true);
        RewardSteps=0;
        setKeepScreenOn(true);
        setFocusableInTouchMode(true);
        play=0;
        steps=1;
    }

    @SuppressLint({"Range", "ResourceAsColor"})
    private void redraw() {
        Canvas mCanvas = surfaceHolder.lockCanvas();
        if(steps>3){
            steps=3;
        }
        //背景
        background.setBounds(0,0,(int)WIDTH,(int)HEIGHT);
        background.draw(mCanvas);
        //蓝方认输按钮
        btn_blue_fail.setBounds((int) blue_fail_minX, (int) blue_fail_minY,
                (int)blue_fail_maxX,(int) blue_fail_maxY);
        btn_blue_fail.draw(mCanvas);
        //红方认输按钮
        btn_red_fail.setBounds((int)red_fail_minX,(int)red_fail_minY,
                (int)red_fail_maxX,(int)red_fail_maxY);
        btn_red_fail.draw(mCanvas);

        Paint paint=new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);

        for(int i=0;i<ROW;i++){
            for(int j=0;j<COL;j++){
                //横线
                mCanvas.drawLine(DistanceLeft,j*Spacing+DistanceTop,length+DistanceLeft,j*Spacing+DistanceTop,paint);
                //竖线
                mCanvas.drawLine(i*Spacing+DistanceLeft,DistanceTop,i*Spacing+DistanceLeft,DistanceTop+length,paint);
                Paint paint1=new Paint();
                paint1.setTextSize(55f);
                if(play==0){
                    paint1.setARGB(240,188,43,63);
                    mCanvas.drawText("红方"+steps+"步",DistanceLeft+Spacing*3/2,DistanceTop+length+Spacing,paint1);
                    mCanvas.drawCircle(DistanceLeft+Spacing,DistanceTop+length+Spacing,50,paint1);
                    if(RewardSteps!=0){
                        mCanvas.drawText("红方奖励"+RewardSteps+"步",DistanceLeft+Spacing*3/2,DistanceTop+length+Spacing*3/2,paint1);
                    }
                }else if(play==1){
                    paint1.setARGB(240,0,150,136);
                    xSteps=(int) (DistanceLeft+length-Spacing*23/8);
                    ySteps=(int) (DistanceTop-Spacing*2/3);
                    switch (steps){
                        case 1:
                            blue_1.setBounds( xSteps,ySteps, (int) (xSteps+Spacing),ySteps+55);
                            blue_1.draw(mCanvas);
                            break;
                        case 2:
                            blue_2.setBounds( xSteps,ySteps, (int) (xSteps+Spacing),ySteps+55);
                            blue_2.draw(mCanvas);
                            break;
                        case 3:
                            blue_3.setBounds( xSteps,ySteps, (int) (xSteps+Spacing),ySteps+55);
                            blue_3.draw(mCanvas);
                            break;
                    }
//                    mCanvas.drawText("蓝方"+steps+"步",DistanceLeft+length-Spacing*23/8,DistanceTop-Spacing*2/3,paint1);
                    mCanvas.drawCircle(DistanceLeft+length-Spacing,DistanceTop-Spacing,50,paint1);
                    if(RewardSteps!=0){
                        xSteps=  (int)(DistanceLeft+length-Spacing*23/8);
                        ySteps= (int)(DistanceTop-Spacing);
                        switch (RewardSteps){
                            case 1:
                                add_blue_1.setBounds(xSteps,ySteps, (int) (xSteps+Spacing*5/4),ySteps+55);
                                add_blue_1.draw(mCanvas);
                                break;
                            case 2:
                                add_blue_2.setBounds(xSteps,ySteps, (int) (xSteps+3*Spacing/2),ySteps+55);
                                add_blue_2.draw(mCanvas);
                                break;
                            case 3:
                                add_blue_3.setBounds(xSteps,ySteps, (int) (xSteps+3*Spacing/2),ySteps+55);
                                add_blue_3.draw(mCanvas);
                                break;
                        }
                    }
                }

            }
        }
        int size=touchXYList.size();
        if(size>0) {
            for(int i=0;i<size;i++){
                int k=i;
                if(k+1==size){
                    mCanvas =drawPiece.draw(mCanvas,startPoint,Spacing,touchXYList.get(i),1);
                }else {
                    mCanvas =drawPiece.draw(mCanvas,startPoint,Spacing,touchXYList.get(i),-1);
                }

            }
        }
        if (mCanvas != null){
            //释放canvas对象并提交画布
            surfaceHolder.unlockCanvasAndPost(mCanvas);
        }
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        new Thread(this::redraw).start();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        HEIGHT = height;
        WIDTH = width;
        length=width-DistanceLeft*2;
        DistanceTop=(HEIGHT-length)/2;
        Spacing=length/5;
        ValidClickAreaLength= Spacing*4/9;
        minX=DistanceLeft-ValidClickAreaLength;
        minY=DistanceTop-ValidClickAreaLength;
        maxX=DistanceLeft+length+ValidClickAreaLength;
        maxY=DistanceTop+length+ValidClickAreaLength;
        red_fail_minX=(WIDTH-2*Spacing)/2;
        red_fail_minY=(HEIGHT-Spacing*3/2);
        red_fail_maxX=(((WIDTH-2*Spacing)/2)+2*Spacing);
        red_fail_maxY=HEIGHT-Spacing*3/2+150;
        blue_fail_maxX=((WIDTH-2*Spacing)/2)+2*Spacing ;
        blue_fail_maxY= (3/2*Spacing)+150;
        blue_fail_minX= (WIDTH-2*Spacing)/2;
        blue_fail_minY= 3/2*Spacing;
        for(int i=0;i<COL;i++){
            XList.add(i*Spacing+DistanceLeft);
        }
        for(int j= 0; j<ROW; j++){
            YList.add(j*Spacing+DistanceTop);
        }
        startPoint=new Point(DistanceLeft,DistanceTop);
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        isDraw=false;
        mSoundPool.Close();
    }

    @Override
    public void run() {
        if(isDraw){
            redraw();
        }
    }
    private Point getFormat(float x,float y) {
        float tempX = -1;
        float tempY = -1;
        if(x> minX&&x<maxX &&y>minY &&y<maxY) {
            for (float X : XList) {
                if (x >= X - ValidClickAreaLength && x < X + ValidClickAreaLength) {
                    tempX = X;
                }
            }
            for (float Y : YList) {
                if (y >= Y - ValidClickAreaLength && y < Y + ValidClickAreaLength) {
                    tempY = Y;
                }
            }
        }
        return new Point(tempX,tempY);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x=event.getX();
        float y=event.getY();
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            mSoundPool.start(0);
            return true;
        }
        if (event.getAction()==MotionEvent.ACTION_UP){
            if(x> minX&&x<maxX &&y>minY &&y<maxY){
                Point tempPoint = getFormat(x, y);
                if(tempPoint.x!=-1&&tempPoint.y!=-1){
                    Point newpoint= new Point((tempPoint.x-startPoint.x)/Spacing,(tempPoint.y-startPoint.y)/Spacing);
                    TouchXY touchXY = new TouchXY(newpoint, play);
                    if(!TouchList.Contains(touchXYList,newpoint)){
                        touchXYList.add(touchXY);
                        steps--;
                        //初始化奖励步数
                        RewardSteps=0;
                        //判断是否符合加步数条件

                        ConditionalJudgment cj=new ConditionalJudgment(newpoint,touchXYList,play,0);
                        if(cj.getSteps()>0){
                            steps+=cj.getSteps();
                            mSoundPool.start(1);
                        }
                        RewardSteps=cj.getSteps();
                        play= changePiece.getChange(steps,play);
                        if(steps==0){
                            steps=1;
                        }
                        int WINCODE=JudgmentWin.JudgmentWin(touchXYList);
                        if(WINCODE> WINSTATUS.NO_END){
                            mDialog mDialog=new mDialog(context,WINCODE);
                            mDialog.DialogShow();
                           initGame();
                        }
                        new Thread(this::run).start();

                    }else{
                        Toast.makeText(context,"这个地方已经由棋子了",Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(context,"棋子要下在交点上",Toast.LENGTH_SHORT).show();
                }

            }else {
                if(x>=red_fail_minX&&x<=red_fail_maxX && y>=red_fail_minY && y<=red_fail_maxY){
                    mDialog mDialog=new mDialog(context,WINSTATUS.BLUE_WIN);
                    mDialog.DialogShow();
                    initGame();
                    new Thread(this::run).start();
                }else if(x>blue_fail_minX && x<=blue_fail_maxX && y>=blue_fail_minY && y<=blue_fail_maxY) {
                    mDialog mDialog = new mDialog(context, WINSTATUS.RED_WIN);
                    mDialog.DialogShow();
                    initGame();
                    new Thread(this::run).start();
                }else {
                    Toast.makeText(context,"请点击棋盘内",Toast.LENGTH_SHORT).show();
                }

            }
        }
        return true;
    }
}
